<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class RSS_Controller extends CI_Controller 
{

	function __construct()
    {
		parent::__construct();
		$this->load->model('product_model');
		$this->load->model('rss_model');
    }


	
	public function index()
	{
		$this->show_recent_rss();
	}
	
	public function show_recent_rss()
	{
		header('Content-Type: text/xml');
		
		$start = rand(0, 1000);
		$data['products'] = $this->product_model->get_recent_products($start, 10); 
 
	
		$this->output->cache(60);
		$this->load->view('product_rss_block', $data);
	}
	
	public function show_rss_using_category($category_name='', $parent_category_name='')
	{
		header('Content-Type: application/rss+xml; charset=utf-8 ');
		$category = $this->product_model->get_category_using_slug_parent_n_category_name($category_name, $parent_category_name);
		
		
		if($category->num_rows() == 1)
		{
			$category = $category->result();
			$category = $category[0];
			// $start = rand(0, $category->product_count - 100);
			$start = 0;
			$data['category'] = $category;
			$products = $this->rss_model->get_products_using_category_id($category->category_id, $start, 100);
			$data['products'] = $products; 

			// write response content to file 
			$this->http_cache_filepath = 'feed';
			$this->http_cache_filename = (empty($parent_category_name) ? '' : "/${parent_category_name}_")."$category_name.rss";
		}
		else
		{
			header('HTTP/1.1 404 Not Found');
		}
		
		
		$this->load->view('product_rss_block', $data);
	}
	
	
	public function show_category_rss_list()
	{
		$this->template->set_template('simple');
		//$this->css_head = array ('/css/main_simple.css');
		$this->template->write_view('header', 'header_detail_block', NULL, FALSE);
		$this->template->write_view('content', 'category_rss_list_block', array('categories' => $this->product_model->get_category_list()), FALSE);	
		$this->template->write_view('footer', 'footer_default_block', NULL, FALSE);
		// $this->output->cache(43200);
		$this->render_template = TRUE;	
	}
	
	public function delete_all_rss()
	{
		$path = "cache/feed/*.rss";
		echo "deleting files from $path ... <BR/>";
		$files = glob($path); 
		foreach($files as $file)
		{
			echo $file.'<BR/>';
			unlink($file); 
		}
	}
	
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/RSS_Controller.php */